var container;

//新增一个可分行整体
function appendContentLine(sizeX, n, aDiv) {
    return contentRandomBinaryDividingAppend(sizeX, parseInt(0.8 * sizeX), n, aDiv);
}

function contentRandomBinaryDividingAppend(x, y, count, aDiv) {  
    if(count > 1) {
        if((parseInt(Math.random() * 10) & 1) == 1) {
            if(y * 2 / x > 3) y /= 2; else x /= 2;
        }
        else {
            if(x * 2 / y > 3) x /= 2; else y /= 2;
        }
    }
    
    if(!appendAContent(x, y, true, aDiv)) return false;
    
    if(count > 1) return contentRandomBinaryDividingAppend(x, y, --count);
    return true;
}

function appendAContent(sizeX, sizeY, left, aDiv) {
    var newDiv;

    if(aDiv != null) {
        newDiv = aDiv;
        newDiv.css("float", (left? "left": "right"));
        newDiv.css("height", (sizeY-12) + "px");
        newDiv.css("width", (sizeX-12) + "px");
        newDiv.css("width", (sizeX-12) + "px");
        newDiv.addClass("content");
    } else newDiv = $("<div class=\"content\" style=\"float:" + (left? "left": "right") + "; height:" + (sizeY-12) + "px; width: " + (sizeX-12) + "px;\"></div>");
    

    $("#contentContainer").append(newDiv);
    return true;
}

function addPlusButton(areaWidth) {
    var divPlus = $("<div id=\"divPlus\" class=\"plus\"></div>");
    
    divPlus.click(function(){
        $("#newDialog").css("display", "flex");
    });
    
    appendContentLine(parseInt(areaWidth * 0.31), 1, divPlus);
}

function divStoreClick() {
    alert(this.value);
}

function addStore(storeID, storeIndexPic) {
    var divStore = $("<div id=\"divPlus\" class=\"store\" value=\"" + storeID + "\"></div>");
    divStore.css("background-image", "url(\"" + storeIndexPic + "\")");

    appendContentLine(parseInt(container.clientWidth * 0.31), 1, divStore);
}

function jsInit() {
    //TODO: 验证登录

    /*alert(JSON.parse("{\"a\":\"1\",\"b\":\"2\"}")["a"]);*/

    //for(var i = 0; i < 3; ++i) appendContentLine(parseInt(container.clientWidth * 0.31), 1, null);

    addPlusButton(container.clientWidth);

    $("#cancel").click(function() {
        $("#newDialog").css("display", "none");
        $("#newDialog").css("background-image", "url(\"img/blank.png\")");
        //.getElementById("storepic").value = "";
    });

    $("#storepic").change(function(){
        var files = this.files;
        if(files.length == 1) {
            $("#newDialog").css("background-image", "url(\"" + URL.createObjectURL(files[0]) + "\")");
        } else {
            $("#newDialog").css("background-image", "url(\"img/blank.png\")");
        }
    });

    document.getElementById("ok").onclick = function() {
        document.getElementById("newStoreData").submit();
    };
}

$(function(){

    var uid   = localStorage["OESuserid"];
    var upass = localStorage["OESloginPass"];

    if(uid == undefined  ||  uid == ""  ||  upass == undefined  ||  upass == "") {
        logout(true);
        return false;
    }

    document.getElementById("userid").value   = uid;
    document.getElementById("password").value = upass;

    container = document.getElementById("contentContainer");

    jsInit();
})
